//引入封装好的axios
import axios from '../../http/axios'
//引入封装的接口api
import apis from '../../http/apis'

//仓库-理赔分仓
export default {
    namespaced: true,
    state: {
        addClaimsData: {
            applyTime: '',
            handleTime: '',
            accidentTime: '',
            ordersId: '',
            images: [],
        },
        claimsListData: [],
        //分页数据
        pages: 0,
        total: 0,
        pageSize: 2,
        currentPage: 1,
        updateClaimsData: {
            _id: '',
            applyTime: '',
            handleTime: '',
            accidentTime: '',
            ordersId: '',
            images: [],
        },
    },
    mutations: {
        setClaimsData(state, addClaimsData) {
            state.addClaimsData = addClaimsData
        },
        setClaimsDataNull(state, Null) {
            state.addClaimsData = Null
        },
        setClaimsListData(state, claimsListData) {
            state.claimsListData = claimsListData
        },
        setUpdateClaimsData(state, updateClaimsData) {
            state.updateClaimsData = updateClaimsData
        },
        //修改pageSize
        setPageSize (state, newPageSize) {
            state.pageSize = newPageSize
        },
        //修改currentPage
        setCurrentPage (state, newCurrentPage) {
            state.currentPage = newCurrentPage
        },
        //修改total
        setTotal (state, total) {
            state.total = total
        },
        //修改pages
        setPages (state, pages) {
            state.pages = pages
        },
        
    },
    actions: {
        //新增理赔信息
        async addClaims(context, addClaimsData) {
            // console.log(addClaimsData);
            let { data } = await apis.claims.addClaims(addClaimsData)
            console.log(data);
        },
        //获取理赔数据
        async getClaims(context) {
            let { data } = await apis.claims.getClaims({
                pageSize: context.state.pageSize,
                currentPage: context.state.currentPage
            })
            console.log(data);
            if (data.code) {
                context.commit('setClaimsListData', data.data.claims)
                context.commit('setTotal', data.data.total)
                context.commit('setPages', data.data.pages)
            }
        },
        //删除理赔数据
        async deleteClaims(context, _id) {
            let { data } = await apis.claims.deleteClaims({ _id })
            //返回给前端 前端做处理
            return data
        },
        //获取某个需要修改的理赔信息
        async getClaimsById(context, _id) {
            let { data } = await apis.claims.getClaimsById({ _id })
            if (data.code) {
                console.log(data.data);
                context.commit('setUpdateClaimsData', data.data)
            }
        },
        //修改理赔信息
        async updateClaims(context, updateClaimsData) {
            let { data } = await apis.claims.updateClaims(updateClaimsData)
            if(data.code){
                context.dispatch('getClaims')
            }
        },
        //修改理赔申请状态
        async againReapply(context, _id) {
            let { data } = await apis.claims.againReapply({_id})
            if(data.code){
                context.dispatch('getClaims')
            }
        },
        //同意理赔申请状态
        async agreeReapply(context, _id) {
            let { data } = await apis.claims.agreeReapply({_id})
            if(data.code){
                context.dispatch('getClaims')
            }
        },
        //拒绝理赔申请状态
        async disagreeReapply(context, _id) {
            let { data } = await apis.claims.disagreeReapply({_id})
            if(data.code){
                context.dispatch('getClaims')
            }
        }
    }
}